﻿#region Using Directives
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Web.UI;
using System.Web.UI.Design;

using CBRSystem.Entities;
using CBRSystem.Data;
using CBRSystem.Data.Bases;
using CBRSystem.Domain;
#endregion

namespace CBRSystem.Web.Data
{
	/// <summary>
	/// Represents the DataRepository.StudentMarkProvider object that provides
	/// data to data-bound controls in multi-tier Web application architectures.
	/// </summary>
	[Designer(typeof(StudentMarkDataSourceDesigner))]
	public class StudentMarkDataSource : ProviderDataSource<StudentMark, StudentMarkKey>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the StudentMarkDataSource class.
		/// </summary>
		public StudentMarkDataSource() : base(new StudentMarkService())
		{
		}

		#endregion Constructors
		
		#region Properties
		
		/// <summary>
		/// Gets a reference to the StudentMarkDataSourceView used by the StudentMarkDataSource.
		/// </summary>
		protected StudentMarkDataSourceView StudentMarkView
		{
			get { return ( View as StudentMarkDataSourceView ); }
		}
		
		/// <summary>
		/// Gets or sets the name of the method or function that
		/// the StudentMarkDataSource control invokes to retrieve data.
		/// </summary>
		public StudentMarkSelectMethod SelectMethod
		{
			get
			{
				StudentMarkSelectMethod selectMethod = StudentMarkSelectMethod.GetAll;
				Object method = ViewState["SelectMethod"];
				if ( method != null )
				{
					selectMethod = (StudentMarkSelectMethod) method;
				}
				return selectMethod;
			}
			set { ViewState["SelectMethod"] = value; }
		}

		#endregion Properties
		
		#region Methods

		/// <summary>
		/// Creates a new instance of the StudentMarkDataSourceView class that is to be
		/// used by the StudentMarkDataSource.
		/// </summary>
		/// <returns>An instance of the StudentMarkDataSourceView class.</returns>
		protected override BaseDataSourceView<StudentMark, StudentMarkKey> GetNewDataSourceView()
		{
			return new StudentMarkDataSourceView(this, DefaultViewName);
		}
		
		/// <summary>
        /// Creates a cache hashing key based on the startIndex, pageSize and the SelectMethod being used.
        /// </summary>
        /// <param name="startIndex">The current start row index.</param>
        /// <param name="pageSize">The current page size.</param>
        /// <returns>A string that can be used as a key for caching purposes.</returns>
		protected override string CacheHashKey(int startIndex, int pageSize)
        {
			return String.Format("{0}:{1}:{2}", SelectMethod, startIndex, pageSize);
        }
		
		#endregion Methods
	}
	
	/// <summary>
	/// Supports the StudentMarkDataSource control and provides an interface for
	/// data-bound controls to perform data operations with business and data objects.
	/// </summary>
	public class StudentMarkDataSourceView : ProviderDataSourceView<StudentMark, StudentMarkKey>
	{
		#region Declarations

		#endregion Declarations
		
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the StudentMarkDataSourceView class.
		/// </summary>
		/// <param name="owner">A reference to the StudentMarkDataSource which created this instance.</param>
		/// <param name="viewName">The name of the view.</param>
		public StudentMarkDataSourceView(StudentMarkDataSource owner, String viewName)
			: base(owner, viewName)
		{
		}
		
		#endregion Constructors
		
		#region Properties

		/// <summary>
		/// Gets a strongly-typed reference to the Owner property.
		/// </summary>
		internal StudentMarkDataSource StudentMarkOwner
		{
			get { return Owner as StudentMarkDataSource; }
		}

		/// <summary>
		/// Gets or sets the name of the method or function that
		/// the DataSource control invokes to retrieve data.
		/// </summary>
		internal StudentMarkSelectMethod SelectMethod
		{
			get { return StudentMarkOwner.SelectMethod; }
			set { StudentMarkOwner.SelectMethod = value; }
		}

		/// <summary>
		/// Gets a strongly typed reference to the Provider property.
		/// </summary>
		internal StudentMarkService StudentMarkProvider
		{
			get { return Provider as StudentMarkService; }
		}

		#endregion Properties
		
		#region Methods
		
		/// <summary>
		/// Gets a collection of Entity objects based on the value of the SelectMethod property.
		/// </summary>
		/// <param name="count">The total number of rows in the DataSource.</param>
		/// <returns>A collection of Entity objects.</returns>
		protected override IList<StudentMark> GetSelectData(out int count)
		{
			Hashtable values = CollectionsUtil.CreateCaseInsensitiveHashtable(GetParameterValues());
			Hashtable customOutput = CollectionsUtil.CreateCaseInsensitiveHashtable();
			IList<StudentMark> results = null;
			StudentMark item;
			count = 0;
			
			System.Int32 _studentMarkId;
			System.Int32 _evaluationComponentId;
			System.Int32 _studentId;
			System.Int32? sp34_StudentId;
			System.Int32? sp34_ComponentId;
			System.Int32? sp31_CourseId;
			System.Int32? sp31_SectionId;
			System.Int32? sp32_SectionId;
			System.Int32? sp30_CourseId;
			System.Int16? sp33_Term;

			switch ( SelectMethod )
			{
				case StudentMarkSelectMethod.Get:
					StudentMarkKey entityKey  = new StudentMarkKey();
					entityKey.Load(values);
					item = StudentMarkProvider.Get(entityKey);
					results = new TList<StudentMark>();
					if ( item != null ) results.Add(item);
					count = results.Count;
					break;
				case StudentMarkSelectMethod.GetAll:
                    results = StudentMarkProvider.GetAll(StartIndex, PageSize, out count);
                    break;
				case StudentMarkSelectMethod.GetPaged:
					results = StudentMarkProvider.GetPaged(WhereClause, OrderBy, PageIndex, PageSize, out count);
					break;
				case StudentMarkSelectMethod.Find:
					if ( FilterParameters != null )
						results = StudentMarkProvider.Find(FilterParameters, OrderBy, StartIndex, PageSize, out count);
					else
						results = StudentMarkProvider.Find(WhereClause, StartIndex, PageSize, out count);
                    break;
				// PK
				case StudentMarkSelectMethod.GetByStudentMarkId:
					_studentMarkId = ( values["StudentMarkId"] != null ) ? (System.Int32) EntityUtil.ChangeType(values["StudentMarkId"], typeof(System.Int32)) : (int)0;
					item = StudentMarkProvider.GetByStudentMarkId(_studentMarkId);
					results = new TList<StudentMark>();
					if ( item != null ) results.Add(item);
					count = results.Count;
					break;
				// IX
				// FK
				case StudentMarkSelectMethod.GetByEvaluationComponentId:
					_evaluationComponentId = ( values["EvaluationComponentId"] != null ) ? (System.Int32) EntityUtil.ChangeType(values["EvaluationComponentId"], typeof(System.Int32)) : (int)0;
					results = StudentMarkProvider.GetByEvaluationComponentId(_evaluationComponentId, this.StartIndex, this.PageSize, out count);
					break;
				case StudentMarkSelectMethod.GetByStudentId:
					_studentId = ( values["StudentId"] != null ) ? (System.Int32) EntityUtil.ChangeType(values["StudentId"], typeof(System.Int32)) : (int)0;
					results = StudentMarkProvider.GetByStudentId(_studentId, this.StartIndex, this.PageSize, out count);
					break;
				// M:M
				// Custom
				case StudentMarkSelectMethod.LookupByStudentAndComponent:
					sp34_StudentId = (System.Int32?) EntityUtil.ChangeType(values["StudentId"], typeof(System.Int32?));
					sp34_ComponentId = (System.Int32?) EntityUtil.ChangeType(values["ComponentId"], typeof(System.Int32?));
					results = StudentMarkProvider.LookupByStudentAndComponent(sp34_StudentId, sp34_ComponentId, StartIndex, PageSize);
					break;
				case StudentMarkSelectMethod.LookupActiveByCourseAndSection:
					sp31_CourseId = (System.Int32?) EntityUtil.ChangeType(values["CourseId"], typeof(System.Int32?));
					sp31_SectionId = (System.Int32?) EntityUtil.ChangeType(values["SectionId"], typeof(System.Int32?));
					results = StudentMarkProvider.LookupActiveByCourseAndSection(sp31_CourseId, sp31_SectionId, StartIndex, PageSize);
					break;
				case StudentMarkSelectMethod.ListAllActive:
					results = StudentMarkProvider.ListAllActive(StartIndex, PageSize);
					break;
				case StudentMarkSelectMethod.LookupActiveBySection:
					sp32_SectionId = (System.Int32?) EntityUtil.ChangeType(values["SectionId"], typeof(System.Int32?));
					results = StudentMarkProvider.LookupActiveBySection(sp32_SectionId, StartIndex, PageSize);
					break;
				case StudentMarkSelectMethod.LookupActiveByCourse:
					sp30_CourseId = (System.Int32?) EntityUtil.ChangeType(values["CourseId"], typeof(System.Int32?));
					results = StudentMarkProvider.LookupActiveByCourse(sp30_CourseId, StartIndex, PageSize);
					break;
				case StudentMarkSelectMethod.LookupActiveByTerm:
					sp33_Term = (System.Int16?) EntityUtil.ChangeType(values["Term"], typeof(System.Int16?));
					results = StudentMarkProvider.LookupActiveByTerm(sp33_Term, StartIndex, PageSize);
					break;
				default:
					break;
			}

			if ( results != null && count < 1 )
			{
				count = results.Count;

				if ( !String.IsNullOrEmpty(CustomMethodRecordCountParamName) )
				{
					object objCustomCount = EntityUtil.ChangeType(customOutput[CustomMethodRecordCountParamName], typeof(Int32));
					
					if ( objCustomCount != null )
					{
						count = (int) objCustomCount;
					}
				}
			}
			
			return results;
		}
		
		/// <summary>
		/// Gets the values of any supplied parameters for internal caching.
		/// </summary>
		/// <param name="values">An IDictionary object of name/value pairs.</param>
		protected override void GetSelectParameters(IDictionary values)
		{
			if ( SelectMethod == StudentMarkSelectMethod.Get || SelectMethod == StudentMarkSelectMethod.GetByStudentMarkId )
			{
				EntityId = GetEntityKey(values);
			}
		}

		/// <summary>
		/// Performs a DeepLoad operation for the current entity if it has
		/// not already been performed.
		/// </summary>
		internal override void DeepLoad()
		{
			if ( !IsDeepLoaded )
			{
				StudentMark entity = GetCurrentEntity();
				
				if ( entity != null )
				{
					// init transaction manager
					GetTransactionManager();
					// execute deep load method
					StudentMarkProvider.DeepLoad(GetCurrentEntity(), EnableRecursiveDeepLoad);
					// set loaded flag
					IsDeepLoaded = true;
				}
			}
		}

		/// <summary>
		/// Performs a DeepLoad operation on the specified entity collection.
		/// </summary>
		/// <param name="entityList"></param>
		/// <param name="properties"></param>
		internal override void DeepLoad(TList<StudentMark> entityList, ProviderDataSourceDeepLoadList properties)
		{
			// init transaction manager
			GetTransactionManager();
			// execute deep load method
			StudentMarkProvider.DeepLoad(entityList, properties.Recursive, properties.Method, properties.GetTypes());
		}

		#endregion Select Methods
	}
	
	#region StudentMarkDataSourceDesigner

	/// <summary>
	/// Provides design-time support in a design host for the StudentMarkDataSource class.
	/// </summary>
	public class StudentMarkDataSourceDesigner : ProviderDataSourceDesigner<StudentMark, StudentMarkKey>
	{
		/// <summary>
		/// Initializes a new instance of the StudentMarkDataSourceDesigner class.
		/// </summary>
		public StudentMarkDataSourceDesigner()
		{
		}

		/// <summary>
		/// Gets or sets the SelectMethod property.
		/// </summary>
		public StudentMarkSelectMethod SelectMethod
		{
			get { return ((StudentMarkDataSource) DataSource).SelectMethod; }
			set { SetPropertyValue("SelectMethod", value); }
		}

		/// <summary>Gets the designer action list collection for this designer.</summary>
		/// <returns>The <see cref="T:System.ComponentModel.Design.DesignerActionListCollection"/>
		/// associated with this designer.</returns>
		public override DesignerActionListCollection ActionLists
		{
			get
			{
				DesignerActionListCollection actions = new DesignerActionListCollection();
				actions.Add(new StudentMarkDataSourceActionList(this));
				actions.AddRange(base.ActionLists);
				return actions;
			}
		}
	}

	#region StudentMarkDataSourceActionList

	/// <summary>
	/// Supports the StudentMarkDataSourceDesigner class.
	/// </summary>
	internal class StudentMarkDataSourceActionList : DesignerActionList
	{
		private StudentMarkDataSourceDesigner _designer;

		/// <summary>
		/// Initializes a new instance of the StudentMarkDataSourceActionList class.
		/// </summary>
		/// <param name="designer"></param>
		public StudentMarkDataSourceActionList(StudentMarkDataSourceDesigner designer) : base(designer.Component)
		{
			_designer = designer;
		}

		/// <summary>
		/// Gets or sets the SelectMethod property.
		/// </summary>
		public StudentMarkSelectMethod SelectMethod
		{
			get { return _designer.SelectMethod; }
			set { _designer.SelectMethod = value; }
		}

		/// <summary>
		/// Returns the collection of <see cref="T:System.ComponentModel.Design.DesignerActionItem"/>
		/// objects contained in the list.
		/// </summary>
		/// <returns>A <see cref="T:System.ComponentModel.Design.DesignerActionItem"/>
		/// array that contains the items in this list.</returns>
		public override DesignerActionItemCollection GetSortedActionItems()
		{
			DesignerActionItemCollection items = new DesignerActionItemCollection();
			items.Add(new DesignerActionPropertyItem("SelectMethod", "Select Method", "Methods"));
			return items;
		}
	}

	#endregion StudentMarkDataSourceActionList
	
	#endregion StudentMarkDataSourceDesigner
	
	#region StudentMarkSelectMethod
	
	/// <summary>
	/// Enumeration of method names available for the StudentMarkDataSource.SelectMethod property.
	/// </summary>
	public enum StudentMarkSelectMethod
	{
		/// <summary>
		/// Represents the Get method.
		/// </summary>
		Get,
		/// <summary>
		/// Represents the GetAll method.
		/// </summary>
		GetAll,
		/// <summary>
		/// Represents the GetPaged method.
		/// </summary>
		GetPaged,
		/// <summary>
		/// Represents the Find method.
		/// </summary>
		Find,
		/// <summary>
		/// Represents the GetByStudentMarkId method.
		/// </summary>
		GetByStudentMarkId,
		/// <summary>
		/// Represents the GetByEvaluationComponentId method.
		/// </summary>
		GetByEvaluationComponentId,
		/// <summary>
		/// Represents the GetByStudentId method.
		/// </summary>
		GetByStudentId,
		/// <summary>
		/// Represents the LookupByStudentAndComponent method.
		/// </summary>
		LookupByStudentAndComponent,
		/// <summary>
		/// Represents the LookupActiveByCourseAndSection method.
		/// </summary>
		LookupActiveByCourseAndSection,
		/// <summary>
		/// Represents the ListAllActive method.
		/// </summary>
		ListAllActive,
		/// <summary>
		/// Represents the LookupActiveBySection method.
		/// </summary>
		LookupActiveBySection,
		/// <summary>
		/// Represents the LookupActiveByCourse method.
		/// </summary>
		LookupActiveByCourse,
		/// <summary>
		/// Represents the LookupActiveByTerm method.
		/// </summary>
		LookupActiveByTerm
	}
	
	#endregion StudentMarkSelectMethod

	#region StudentMarkFilter
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="SqlFilter&lt;EntityColumn&gt;"/> class
	/// that is used exclusively with a <see cref="StudentMark"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class StudentMarkFilter : SqlFilter<StudentMarkColumn>
	{
	}
	
	#endregion StudentMarkFilter

	#region StudentMarkExpressionBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="SqlExpressionBuilder&lt;EntityColumn&gt;"/> class
	/// that is used exclusively with a <see cref="StudentMark"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class StudentMarkExpressionBuilder : SqlExpressionBuilder<StudentMarkColumn>
	{
	}
	
	#endregion StudentMarkExpressionBuilder	

	#region StudentMarkProperty
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="ChildEntityProperty&lt;StudentMarkChildEntityTypes&gt;"/> class
	/// that is used exclusively with a <see cref="StudentMark"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class StudentMarkProperty : ChildEntityProperty<StudentMarkChildEntityTypes>
	{
	}
	
	#endregion StudentMarkProperty
}

